package com.example.demo.service.impl;

import com.example.demo.common.enums.CustomizeEnum;
import com.example.demo.common.enums.StaticValues;
import com.example.demo.entity.User;
import com.example.demo.service.impl.UserServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

/**
 * @Author: ak
 * @Date: 2020/4/1 23:37
 */
@Service
@Slf4j
public class LoginService {
    @Autowired
    UserServiceImpl userService;

    @Value("${user.avatar}")
    String userAvatar;

    public CustomizeEnum userLogin(String username, String password) {
        String userRole = userService.getUserRole(username);
        if (userRole == null) {
            return CustomizeEnum.USERNAME_OR_PASSWORD_ERROR;
        }
        Subject subject = SecurityUtils.getSubject();
        UsernamePasswordToken token = new UsernamePasswordToken(username, password);
        // 执行认证登陆
        try {
            subject.login(token);
        } catch (UnknownAccountException uae) {
            return CustomizeEnum.LOGIN_UNKNOWN_ACCOUNT;
        } catch (IncorrectCredentialsException ice) {
            return CustomizeEnum.LOGIN_PASSWORD_ERROR;
        } catch (LockedAccountException lae) {
            return CustomizeEnum.LOGIN_ACCOUNT_LOCK;
        } catch (ExcessiveAttemptsException eae) {
            return CustomizeEnum.LOGIN_ERROR_COUNT_OVERMUCH;
        } catch (AuthenticationException ae) {
            return CustomizeEnum.USERNAME_OR_PASSWORD_ERROR;
        }
        if (subject.isAuthenticated()) {
            if (userRole.equals(StaticValues.ADMIN)) {
                return CustomizeEnum.LOGIN_ADMIN;
            } else if (userRole.equals(StaticValues.USER)) {
                return CustomizeEnum.LOGIN_USER;
            } else {
                return CustomizeEnum.LOGIN_UNKNOWN_ACCOUNT;
            }
        } else {
            token.clear();
            return CustomizeEnum.LOGIN_ERROR;
        }
    }

    public CustomizeEnum register(String username, String password, String password2) {
        if (!checkAccount(username,password)){
            return CustomizeEnum.REGISTER_ERROR;
        }
        if (!password.equals(password2)) {
            return CustomizeEnum.REGISTER_PASSWORD_ERROR;
        }
        if (userService.getUserRole(username) != null) {
            return CustomizeEnum.REGISTER_USER_EXIST;
        }
        User user = new User();
        user.setUUsername(username);
        user.setUPassword(password);
        user.setURole("user");
        user.setUCreate(String.valueOf(System.currentTimeMillis()));
        user.setULv(1);
        user.setUAvatar(userAvatar);
        userService.save(user);
        return CustomizeEnum.REGISTER_SUCCESS;
    }

    private boolean checkAccount(String username, String password){
        String regex = "^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{8,16}$";
        return username.matches(regex) && password.matches(password);
    }
}
